package com.thinkfit.jy.fragment;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.AppUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.ApplicationActivity;
import com.thinkfit.jy.activity.FreeMovementActivity;
import com.thinkfit.jy.activity.ProceduralTrainingActivity;
import com.thinkfit.jy.activity.VideoActivity;
import com.thinkfit.jy.adapter.AppAdapter;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 泰诺健风格主题首页菜单界面
 */
public class HomeFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {



    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.flVideoCourse, R.id.flSport,
                R.id.flLiveVideo, R.id.flTv, R.id.flApplication,R.id.fl3DCourse);
        if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)){
            setImage(R.id.ivVideoCourse, R.drawable.app_video_course_2);
            setImage(R.id.ivLiveVideo, R.drawable.app_live_video_2);
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)){
            setImage(R.id.ivVideoCourse, R.drawable.app_video_course_1);
            setImage(R.id.ivLiveVideo, R.drawable.app_live_video);
        }

        ArrayList<AppUtils.AppInfo> list1 = new ArrayList<>();
        ArrayList<AppUtils.AppInfo> list2 = new ArrayList<>();
        PackageManager pm = getActivity().getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        int size = packages.size();
        int position = 0;
        for (int i = 0; i < size; i++) {
            PackageInfo packageInfo = packages.get(i);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0 ||
                    packageInfo.packageName.equals(getActivity().getPackageName())) {
                continue;
            }
            if (position % 2 == 0) {
                list1.add(new AppUtils.AppInfo(
                        packageInfo.packageName,
                        packageInfo.applicationInfo.loadLabel(pm).toString(),
                        packageInfo.applicationInfo.loadIcon(pm),
                        "",
                        packageInfo.versionName,
                        packageInfo.versionCode,
                        false
                ));
            } else {
                list2.add(new AppUtils.AppInfo(
                        packageInfo.packageName,
                        packageInfo.applicationInfo.loadLabel(pm).toString(),
                        packageInfo.applicationInfo.loadIcon(pm),
                        "",
                        packageInfo.versionName,
                        packageInfo.versionCode,
                        false
                ));
            }
            ++position;
        }
        initApplication(view.findViewById(R.id.recyclerView1), list1);
        initApplication(view.findViewById(R.id.recyclerView2), list2);

    }

    private void initApplication(RecyclerView recyclerView, ArrayList<AppUtils.AppInfo> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        AppAdapter appAdapter = new AppAdapter(getActivity());
        appAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                openActivity(ApplicationActivity.class);
            }
        });
        appAdapter.onAttachedToRecyclerView(recyclerView);
        recyclerView.setAdapter(appAdapter);
        appAdapter.setNewInstance(list);
    }

    @Override
    public void onResume() {
        super.onResume();

        if(!BluetoothOrder.getInstance().isConnect()){
            setImage(R.id.ivVideoCourse,R.drawable.app_video_course);
            setImage(R.id.ivLiveVideo, R.drawable.app_live_video);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.flVideoCourse://视频课程
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                openActivity(VideoActivity.class, bundle);
                break;
            case R.id.flSport://目标训练
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 0);
                openActivity(FreeMovementActivity.class, bundle);
                break;
            case R.id.flLiveVideo://实景视频
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 4);
                openActivity(VideoActivity.class, bundle);
                break;
            case R.id.flApplication://应用管理
                openActivity(ApplicationActivity.class);
                break;
            case R.id.flTv://TV
//                if(DeviceTypeUtils.getInstance().isInstallApp(getContext(),DeviceTypeUtils.CCTV)){
//                    DeviceTypeUtils.getInstance().openApp(getContext(),DeviceTypeUtils.CCTV);
//                    return;
//                }else if(DeviceTypeUtils.getInstance().isInstallApp(getContext(),DeviceTypeUtils.XINGHUO)){
//                    DeviceTypeUtils.getInstance().openApp(getContext(),DeviceTypeUtils.XINGHUO);
//                    return;
//                }else if(DeviceTypeUtils.getInstance().isInstallApp(getContext(),DeviceTypeUtils.YINGSHICANG)){
//                    DeviceTypeUtils.getInstance().openApp(getContext(),DeviceTypeUtils.YINGSHICANG);
//                    return;
//                }else if(DeviceTypeUtils.getInstance().isInstallApp(getContext(),DeviceTypeUtils.TENGXUN)){
//                    DeviceTypeUtils.getInstance().openApp(getContext(),DeviceTypeUtils.TENGXUN);
//                    return;
//                }else {
//                    ToastUtils.showShort("应用未安装");
//                }
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                openActivity(FreeMovementActivity.class, bundle);
//                openActivity(TVActivity.class);
                break;
            case R.id.fl3DCourse://程序课程
//                ToastUtils.showShort(getString(R.string.app_toast_hint_2));
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                openActivity(ProceduralTrainingActivity.class,bundle);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(@NonNull HashMap<String, Object> map) {
        super.onReceive(map);
        if(map.containsKey(EventBusEvent.REFRESH_FRAG)){
            if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)){
                setImage(R.id.ivVideoCourse, R.drawable.app_video_course_2);
                setImage(R.id.ivLiveVideo, R.drawable.app_live_video_2);
            }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)){
                setImage(R.id.ivVideoCourse, R.drawable.app_video_course_1);
                setImage(R.id.ivLiveVideo, R.drawable.app_live_video);
            }else {
                setImage(R.id.ivVideoCourse, R.drawable.app_video_course);
                setImage(R.id.ivLiveVideo, R.drawable.app_live_video);
            }
        }
    }


}
